
//
//  NSDictionary+HSDicTool.m
//  SDK_js
//
//  Created by Mine on 16/7/28.
//  Copyright © 2016年 min. All rights reserved.
//

#import "NSDictionary+HSDicTool.h"

@implementation NSDictionary (HSDicTool)
/**
    字典转json格式字符串
 */
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    if (!dic) {
        return @"";
    }
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

/**
    json格式字符串 转 字典转
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        
        return nil;
        
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    
    if(err) {
        
        HSLog(@"json解析失败：%@",err);
        
        return nil;

    }
    
    return dic;
    
}

/**
 json格式字符串 转 字典转
 */
+ (NSArray *)dicArrWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil || [jsonString isEqual:[NSNull null]]) {
        
        return nil;
        
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err;
    
    NSArray * arr = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    
    if(err) {
        
        //(@"json解析失败：%@",err);
        
        return nil;
        
    }
    
    return arr;
    
}
@end
